package zhoug.common.view.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import zhoug.common.R;
import zhoug.logging.Logger;

import androidx.annotation.AttrRes;
import androidx.annotation.StyleRes;

/**
 * @Author 35574
 * @Date 2020/7/22
 * @Description
 */
class DialogAttributeParser {
    private static final String TAG = ">>>DialogAttributeParser";
    private static boolean debug = false;

    public static void setDebug(boolean debug) {
        DialogAttributeParser.debug = debug;
    }

    /**
     * {@link R.styleable#ZBaseDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZBaseDialogAttrs(ZBaseDialog dialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (dialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZBaseDialog, defStyleAttr, defStyleRes);
        int corners = a.getDimensionPixelOffset(R.styleable.ZBaseDialog_zg_corners, 0);
        boolean fullWidth = a.getBoolean(R.styleable.ZBaseDialog_zg_fullWidth, false);
        int titleColor = a.getColor(R.styleable.ZBaseDialog_zg_titleColor, 0);
        float titleSize = a.getDimension(R.styleable.ZBaseDialog_zg_titleSize, 0);
        int messageColor = a.getColor(R.styleable.ZBaseDialog_zg_messageColor, 0);
        float messageSize = a.getDimension(R.styleable.ZBaseDialog_zg_messageSize, 0);
        int maxHeight = a.getDimensionPixelOffset(R.styleable.ZBaseDialog_zg_maxHeight, 0);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZBaseDialog:corners=" + corners);
            Logger.d(TAG, "ZBaseDialog:fullWidth=" + fullWidth);
            Logger.d(TAG, "ZBaseDialog:titleColor=" + Integer.toHexString(titleColor));
            Logger.d(TAG, "ZBaseDialog:titleSize=" + titleSize);
            Logger.d(TAG, "ZBaseDialog:messageColor=" + Integer.toHexString(messageColor));
            Logger.d(TAG, "ZBaseDialog:messageSize=" + messageSize);
            Logger.d(TAG, "ZBaseDialog:maxHeight=" + maxHeight);
        }
        dialog.setCorners(corners);
        dialog.setFullWidth(fullWidth);
        dialog.setTitleColor(titleColor);
        dialog.setTitleSize(titleSize);
        dialog.setMessageColor(messageColor);
        dialog.setMessageSize(messageSize);
        dialog.setMaxHeight(maxHeight);

    }

    /**
     * {@link R.styleable#ZAbsListDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZMUIAbsListDialogAttrs(ZAbsListDialog dialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (dialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZAbsListDialog, defStyleAttr, defStyleRes);
        float textSize = a.getDimension(R.styleable.ZAbsListDialog_zg_textSize, 0);
        int textColor = a.getColor(R.styleable.ZAbsListDialog_zg_textColor, 0);
        int dividerHeight = a.getDimensionPixelOffset(R.styleable.ZAbsListDialog_zg_dividerHeight, 1);
        int dividerColor = a.getColor(R.styleable.ZAbsListDialog_zg_dividerColor, Color.parseColor("#dddddd"));
        boolean showDivider = a.getBoolean(R.styleable.ZAbsListDialog_zg_showDivider, true);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZMUIAbsListDialog:textSize=" + textSize);
            Logger.d(TAG, "ZMUIAbsListDialog:textColor=" + Integer.toHexString(textColor));
            Logger.d(TAG, "ZMUIAbsListDialog:dividerHeight=" + dividerHeight);
            Logger.d(TAG, "ZMUIAbsListDialog:dividerColor=" + Integer.toHexString(dividerColor));
            Logger.d(TAG, "ZMUIAbsListDialog:showDivider=" + showDivider);
        }
        dialog.setTextSize(textSize);
        dialog.setTextColor(textColor);
        dialog.setDividerColor(dividerColor);
        dialog.setDividerHeight(dividerHeight);
        dialog.setShowDivider(showDivider);

    }

    /**
     * {@link R.styleable#ZButtonDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZButtonDialogAttrs(ZButtonDialog dialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (dialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZButtonDialog, defStyleAttr, defStyleRes);
        int positiveBtnColor = a.getColor(R.styleable.ZButtonDialog_zg_positiveBtnColor, 0);
        float positiveBtnSize = a.getDimension(R.styleable.ZButtonDialog_zg_positiveBtnSize, 0);
        int negativeBtnColor = a.getColor(R.styleable.ZButtonDialog_zg_negativeBtnColor, 0);
        float negativeBtnSize = a.getDimension(R.styleable.ZButtonDialog_zg_negativeBtnSize, 0);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZMUIButtonDialog:positiveBtnSize=" + positiveBtnSize);
            Logger.d(TAG, "ZMUIButtonDialog:positiveBtnColor=" + Integer.toHexString(positiveBtnColor));
            Logger.d(TAG, "ZMUIButtonDialog:negativeBtnSize=" + negativeBtnSize);
            Logger.d(TAG, "ZMUIButtonDialog:negativeBtnColor=" + Integer.toHexString(negativeBtnColor));
        }
        dialog.setPositiveBtnColor(positiveBtnColor);
        dialog.setPositiveBtnSize(positiveBtnSize);
        dialog.setNegativeBtnColor(negativeBtnColor);
        dialog.setNegativeBtnSize(negativeBtnSize);

    }

}
